import axios from 'axios'
import functions from "./functions"
import qs from "qs";

// 创建axios实例
const httpService = axios.create({
    //API地址
    baseURL: '/index.php/api/',
    // 请求超时时间
    timeout: 30000,
    withCredentials: true
});

// request拦截器
httpService.interceptors.request.use(
    config => {
        config.headers['Accept-Language'] = window.MyAPP.$store.state.lang;
        if (config.method === 'post') {
            let _data = qs.parse(config.data);
            if (_data.showLoading !== false) {
                window.MyAPP.$store.commit('loading', true)
            }
        } else {
            if (config.params.showLoading !== false) {
                window.MyAPP.$store.commit('loading', true)
            }
        }
        return config;
    },
    error => {
        // 请求错误处理
        Promise.reject(error);
    }
);

// response拦截器
httpService.interceptors.response.use(
    response => {
        // 统一处理状态
        if (response.status === 200) {
            window.MyAPP.$store.commit('loading', false)
            return response.data
        }
        return false;
    },
    // 处理处理
    error => {
        window.MyAPP.$store.commit('loading', false)
        if (error && error.response) {
            if (error.response.status === 401 || error.response.status === 600) {
                functions.showWarning(window.MyAPP.$t('common.reLogin'))
                window.MyAPP.$cookies.remove('token')
                window.MyAPP.$cookies.remove('uid')
                window.MyAPP.$cookies.remove('nickname')
                window.MyAPP.$router.push('/login')
            } else {
                functions.showWarning(window.MyAPP.$t('common.serverBusy'))
            }
        } else {
            error.message = window.MyAPP.$t('common.requestError')

            functions.showError(error.message)
            return false
        }
        return false;
    }
);

/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            params: params
        }).then(response => {
            if (response) {
                if (response.code === 1) {
                    if (response.msg) {
                        functions.showSuccess(response.msg)
                    }
                    resolve(response.data);
                    return;
                }

                if (response.code !== 0) {
                    resolve(response);
                    return;
                }

                functions.showError(response.msg)
                return false
            }
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: qs.stringify(params)
        }).then(response => {
            if (response) {
                if (response.code === 1) {
                    if (response.msg) {
                        functions.showSuccess(response.msg)
                    }
                    resolve(response.data);
                    return;
                }

                if (response.code !== 0) {
                    resolve(response);
                    return;
                }

                functions.showError(response.msg)
                return false
            }
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: {'Content-Type': 'multipart/form-data'}
        }).then(response => {
            if (response) {
                if (response.code === 1) {
                    if (response.msg) {
                        functions.showSuccess(response.msg)
                    }
                    resolve(response.data);
                    return;
                }

                if (response.code !== 0) {
                    resolve(response);
                    return;
                }

                functions.showError(response.msg)
                return false
            }
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

export default {
    get,
    post,
    fileUpload
}